import VPlay 2.0
import QtQuick 2.0

import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1

import VPlayApps 1.0
import "../common"

import QtWebSockets 1.0

SceneBase {
  id: setting

  property bool dpMode: true
  property var paths: []

  signal sendPathCmd(int cmd, int number, int dir)

  Rectangle {
    z: -2
    anchors.fill: parent.gameWindowAnchorItem
    color: "#47688e"
  }
  MenuButton {
    text: "返回"
    anchors.right: setting.gameWindowAnchorItem.right
    anchors.rightMargin: 10
    anchors.top: setting.gameWindowAnchorItem.top
    anchors.topMargin: 10
    onClicked: {
      backButtonPressed()
    }
  }
  MenuButton {
    id: add_button
    text: "+"
    anchors.left: setting.gameWindowAnchorItem.left
    anchors.leftMargin: 10
    anchors.top: setting.gameWindowAnchorItem.top
    anchors.topMargin: 10

    property string userName: "1"

    onClicked: {
      nativeUtils.displayTextInput("填写编号:", "", "", add_button.userName);
    }
    Connections {
      target: nativeUtils
      onTextInputFinished: {
        // if the input was confirmed with Ok, store the userName as the property
        if(accepted) {
          add_button.userName = enteredText
          paths.push({
                       text: add_button.userName,
                       detailText: "这里是送餐机器人路线规划：",
                       icon: IconType.road,
                       path: []
                     })
          listview.model = paths
          sendPathCmd(parseInt(add_button.userName.toString()), 0, 0)
          //console.log("Entered userName:", add_button.userName)
        }
      }
    }
  }


  ListView {
    id: listview
    z: -1
    // make it take the whole window, not only the logical scene size 480x320
    anchors.fill: setting.gameWindowAnchorItem
    //anchors.top: add_button.bottom
    anchors.topMargin: add_button.height + add_button.anchors.topMargin + 10

    model: paths

    delegate: SwipeOptionsContainer {
      id: container
      //height: row.height

      //enabled: !(isSnapped && isRight)

      width: parent.width
      height: dpMode ? setting.dp(70) : 60
//      //the actual list item
      SimpleRow {
        id: row
        //text: modelData
        style.showDisclosure: false
        onSelected: {
          nativeUtils.displayMessageBox("删除", "", 3)
        }
        Connections {
          target: nativeUtils
          onMessageBoxFinished: {
            if (accepted) {
              paths.splice(index, 1)
              listview.model = paths
              sendPathCmd(parseInt(modelData.text), 1, 0)
            }

            // only quit, if the activeScene is menuScene - the messageBox might also get opened from other scenes in your code
            //console.log("onMessageBoxFinished = ", accepted)
          }

        }
      }

      //left swipe option (when swiping list item to right)
      leftOption: SwipeButton {
        //text: "修改"
        //textSize: 18
        icon: IconType.arrowleft
        height: row.height
        onClicked: {
          row.item.detailText += "左"
          row.item.path.push(0);
          sendPathCmd(parseInt(row.item.text), 2, 0)
          //row.item.text = "Option clicked"
          row.itemChanged()

          //hide left option when clicked
          container.hideOptions()
        }
      }

      //right swipe option (when swiping list item to left)
      rightOption: SwipeButton {
        //text: "删除"
        //textSize: 18
        icon: IconType.arrowright
        height: row.height
        onClicked: {
          row.item.detailText += "右"
          row.item.path.push(1);
          sendPathCmd(parseInt(row.item.text), 2, 1)
          //paths.splice(index, 1)
          //listview.model = paths
          //row.text = "rightOption clicked"
          row.itemChanged()

          //hide left option when clicked
          container.hideOptions()
        }
      }
      //hide right option after timer finishes
      onRightOptionShown: hideTimer.start()

      property Timer hideTimer: Timer {
        running: false
        interval: 1000
        onTriggered: container.hideOptions()
      }


    }
  }// ListView

  WebSocket {
    id: socket
    url: ""
    onTextMessageReceived: {

    }
    onStatusChanged: {
      if (socket.status == WebSocket.Error) {

      } else if (socket.status == WebSocket.Closed) {

      } else if (socket.status == WebSocket.Open) {
        socket.sendTextMessage(qsTr("i am controller!!"));
        socket.active = false
      }
    }
  }



}
